
#include <BEvent>
#include <BChangeEvent>
#include <BPainter>
#include <BImage>
#include <BFont>
#include <BStyle>
#include <BStyle>

#include <BLabel>
#include "member_BLabel.h"

using namespace BWE;

#define member					(*(member_BLabel*)_ptr)
#define member_allocate()		_ptr = new member_BLabel(this)
#define member_release()		delete (member_BLabel*)_ptr

BLabel::BLabel(BWidget* parent) : BWidget(parent)
{
	member_allocate();
}
BLabel::BLabel(const BString& text, BWidget* parent) : BWidget(parent)
{
	member_allocate();
	setText(text);
}
BLabel::BLabel(BImage* image, BWidget* parent) : BWidget(parent)
{
	member_allocate();
	setImage(image);
}
BLabel::BLabel(BImage* image, const BString& text, BWidget* parent) : BWidget(parent)
{
	member_allocate();
	setImage(image);
	setText(text);
}
BLabel::~BLabel()
{
	member_release();
}

void BLabel::setText(const BString& text)
{
	if(member.text != text)
	{
		member.text = text;
		this->fresh();
		emit(Signal_TextChanged, member.text);
	}
}
const BString& BLabel::text() const
{
	return member.text;
}

void BLabel::setImage(BImage* image)
{
	if(member.image != image)
	{
		member.image = image;
		this->fresh();
		emit(Signal_ImageChanged, member.image.ptr());
	}
}
BImage* BLabel::image()
{
	return member.image.ptr();
}
const BImage* BLabel::image() const
{
	return member.image.ptr();
}

void BLabel::setAlign(Align align)
{
	if (member.align != align)
	{
		member.align = align;
		this->fresh();
		emit(Signal_ALignChanged, align);
	}
}
Align BLabel::align() const
{
	return member.align;
}

BSize BLabel::sizeHint() const
{
	BSize textSize(16, 16);
	const BFont* font = this->font();
	if(font)
	{
		textSize = font->textSize(member.text);
	}
	return this->shellSize() + textSize;
}

void BLabel::changeEvent(const BChangeEvent& changeEvent)
{
	if (changeEvent.change() == Change_Style)
	{

	}
}
void BLabel::freshEvent(const BEvent& event)
{
	member.freshRects();
}
void BLabel::paintEvent(const BEvent& event)
{
	BRect crect = this->clientRect();

	BPainter painter(this);
	painter.drawBackground();
	painter.drawClient();

	painter.drawPerch();

	if (member.image)
	{
		painter.drawImage(crect, member.image);
	}
	if (painter.setColor(Color_Text))
	{
		if (member.t_text.size())
			painter.drawText(member.textRect, member.t_text);
		else
			painter.drawText(member.textRect, member.text);

		painter.drawMargin();
		painter.drawBorder();
	}
}
